package util

import (
	"bytes"
	"fmt"
	"net/url"
	"sort"
)

/**
短信push时post参数进行urlEncode，只对value进行编码
*/
func CustomEncode(v map[string]interface{}) string {
	if v == nil {
		return ""
	}

	var buf bytes.Buffer
	len := len(v)
	keys := make([]string, 0, len)
	for k := range v {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for index, k := range keys {
		vs := v[k]
		if index == len-1 {
			buf.WriteString(fmt.Sprintf("%s=%s", k, url.QueryEscape(vs.(string))))
		} else {
			buf.WriteString(fmt.Sprintf("%s=%s&", k, url.QueryEscape(vs.(string))))
		}
	}

	return buf.String()
}
